namespace MoreLinq;

using System;
using System.Collections.Generic;
using Experimental;

public static partial class MoreEnumerable
{
    /// <summary>
    /// Repeats the sequence the specified number of times.
    /// </summary>
    /// <typeparam name="T">Type of elements in sequence</typeparam>
    /// <param name="sequence">The sequence to repeat</param>
    /// <param name="count">重复次数,如果count为空则抛出ArgumentNullException</param>
    /// <returns>A sequence produced from the repetition of the original source sequence</returns>

    public static IEnumerable<T> Repeat<T>(this IEnumerable<T> sequence, int count)
    {
            if (sequence == null) throw new ArgumentNullException(nameof(sequence));
            if (count < 0) throw new ArgumentOutOfRangeException(nameof(count), "Repeat count must be greater than or equal to zero.");
            return RepeatImpl(sequence, count);
        }

    /// <summary>
    /// Repeats the sequence forever.
    /// </summary>
    /// <typeparam name="T">Type of elements in sequence</typeparam>
    /// <param name="sequence">The sequence to repeat</param>
    /// <returns>A sequence produced from the infinite repetition of the original source sequence</returns>

    public static IEnumerable<T> Repeat<T>(this IEnumerable<T> sequence)
    {
            if (sequence == null) throw new ArgumentNullException(nameof(sequence));
            return RepeatImpl(sequence, null);
        }


    static IEnumerable<T> RepeatImpl<T>(IEnumerable<T> sequence, int? count)
    {
            var memo = sequence.Memoize();
            using (memo as IDisposable)
            {
                while (count == null || count-- > 0)
                {
#pragma warning disable CA1851 // Possible multiple enumerations of 'IEnumerable' collection
                    foreach (var item in memo)
#pragma warning restore CA1851 // Possible multiple enumerations of 'IEnumerable' collection
                    {
                        yield return item;
                    }
                }
            }
        }
}
